package com.diankangwo.controller;

import com.diankangwo.constant.PayStatus;
import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.OrderDAO;
import com.diankangwo.dao.ShoppingCartDAO;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.Order;
import com.diankangwo.domain.RefundMoney;
import com.diankangwo.domain.UserInfo;
import com.diankangwo.service.RefundMoneyService;
import com.diankangwo.util.BeecloudPayUtil;
import com.diankangwo.util.OrderUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退费记录控制器
 *
 * @author xiaoyu
 */
@Controller
public class RefundMoneyController {

    // 日志记录
    private static final Logger LOG = Logger.getLogger(UserController.class);

    @Autowired
    private RefundMoneyService refundMoneyService;// 注入services
    @Autowired
    private UserDAO userDao;
    @Autowired
    private OrderDAO orderDAO;
    @Autowired
    private ShoppingCartDAO shoppingCartDAO;

    /**
     * 退款
     *
     * @param accessToken token
     * @return
     */
    @ApiOperation(value = " 退款", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "rm", value = "可选 mine / saleing", required = true, dataType = "RefundMoney")
    })
    @ResponseBody
    @RequestMapping(value = "/RefundMoney/insert", method = RequestMethod.POST)
    public String insertOrUpdate(RefundMoney rm, String accessToken) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            Order order = orderDAO.findByOrderNo(rm.getPaymenOrderNo());
            if (order.getUid().equals(user.getUid())) {
                if (order.getOrderStatus().equals(PayStatus.success.getName())) {
                    try {
                        rm.setRefundNo(OrderUtil.getNumberForPK());
                        rm.setChannel(order.getOrderChanel());
                        // 执行操作
                        refundMoneyService.saveOrUpdate(rm);
                        BeecloudPayUtil util = new BeecloudPayUtil();
                        Map<String, Object> map = new HashMap<>();
                        map.put("orderNo", order.getOrderNo());
                        //util.BCRefundParamter(rm.getPaymenOrderNo(),rm.getRefundNo(),rm.getMoney(),map);
                        order.setOrderStatus(PayStatus.refund.getName());
                        orderDAO.saveOrUpdate(order);
                        // 记录日志
                        LOG.debug("新增、更新退费记录成功！");
                    } catch (Exception e) {
                        // 异常 记录日志
                        e.printStackTrace();
                        LOG.error("新增、更新退费记录失败！\n" + e);
                    }
                } else {
                    code = "2";
                    msg = "该订单还未支付，无法退款！";
                }
            } else {
                code = "2";
                msg = "该订单不属于该用户！";
            }
        } else {
            code = "2";
            msg = "token已失效！";
        }
        return resultMsg.mapToJson(code, msg, "");
    }

    /**
     * 查询退费记录
     *
     * @param rm
     * @return
     */
    @RequestMapping(value = "/RefundMoney/findAll", method = RequestMethod.POST)
    public ModelAndView findAll(RefundMoney rm) {
        // 创建model
        ModelAndView view = new ModelAndView();
        // 创建集合，存储数据
        List<RefundMoney> rmList = new ArrayList<RefundMoney>();
        try {
            // 执行操作
            rmList = refundMoneyService.findAll();
            // 记录日志
            LOG.debug("查询退费记录成功！");
            // 将数据存入view,便于前段页面获取
            view.addObject("rmList", rmList);
        } catch (Exception e) {
            // 异常 记录日志
            e.printStackTrace();
            LOG.error("查询退费记录失败！\n" + e);
        }
        return view;
    }

    /**
     * 删除退费记录
     *
     * @param rm
     * @return
     */
    @RequestMapping(value = "/RefundMoney/delete", method = RequestMethod.POST)
    public ModelAndView delete(RefundMoney rm) {
        // 创建model
        ModelAndView view = new ModelAndView();
        // 标识是否成功
        boolean isOk;
        try {
            // 执行操作
            isOk = refundMoneyService.deleteRefunMoney(rm);
            // 判断是否执行成功，记录日志
            if (isOk)
                LOG.debug("删除退费记录成功！");
            else
                LOG.error("删除退费记录失败！\n");
        } catch (Exception e) {
            // 异常 记录日志
            e.printStackTrace();
            LOG.error("删除退费记录失败！\n" + e);
        }
        return view;
    }

    /**
     * 按id删除退费记录
     *
     * @return
     */
    @RequestMapping(value = "/RefundMoney/deleteById", method = RequestMethod.POST)
    public String deleteById(int id) {
        try {
            // 执行操作
            refundMoneyService.deleteById(id);
            // 记录日志
            LOG.debug("删除退费记录成功！");
        } catch (Exception e) {
            // 异常 记录日志
            e.printStackTrace();
            LOG.error("删除退费记录失败！\n" + e);
        }
        return "";
    }
}
